package com.yomahub.liteflow.example.xstream.converter;

import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.ConverterMatcher;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * @program: liteflow-example
 * @description:
 * @author: yyyyjinying
 * @create: 2023-09-25 22:23
 **/
public class SingleValueCalendarConverter implements Converter {

    // 转化的对象 输出的数据 上下文
    @Override
    public void marshal(Object source, HierarchicalStreamWriter writer,
                        MarshallingContext context) {

        Calendar calendar = (Calendar) source;
        writer.setValue(String.valueOf(calendar.getTime().getTime()));
    }
    @Override
    public Object unmarshal(HierarchicalStreamReader reader,
                            UnmarshallingContext context) {
        GregorianCalendar calendar = new GregorianCalendar();
        calendar.setTime(new Date(Long.parseLong(reader.getValue())));
        return calendar;
    }
    @Override
    public boolean canConvert(Class type) {
        return type.equals(GregorianCalendar.class);
    }
}